package com.smartscreen.attendancemanage.service.impl;

import com.smartscreen.attendancemanage.dto.CreateAttendanceRuleRequest;
import com.smartscreen.attendancemanage.entity.AttendanceRule;
import com.smartscreen.attendancemanage.mapper.AttendanceRuleMapper;
import com.smartscreen.attendancemanage.service.AttendanceRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.util.List;
import java.util.UUID;

@Service
public class AttendanceRuleServiceImpl implements AttendanceRuleService {
    
    @Autowired
    private AttendanceRuleMapper attendanceRuleMapper;
    
    @Override
    public String createAttendanceRule(CreateAttendanceRuleRequest request) {
        String ruleId = "rule_" + UUID.randomUUID().toString().substring(0, 8);
        
        AttendanceRule rule = new AttendanceRule();
        rule.setRuleId(ruleId);
        rule.setName(request.getName());
        rule.setType(request.getType());
        rule.setStartTime(LocalTime.parse(request.getStartTime()));
        rule.setEndTime(LocalTime.parse(request.getEndTime()));
        rule.setRepeatDays(String.join(",", request.getRepeatDays()));
        rule.setStatus("active");
        
        attendanceRuleMapper.insert(rule);
        
        // 这里还需要插入位置关联数据
        // 为了简化，暂时省略
        
        return ruleId;
    }
    
    @Override
    public List<AttendanceRule> getAllAttendanceRules() {
        return attendanceRuleMapper.selectList(null);
    }
    
    @Override
    public void updateAttendanceRule(String ruleId, CreateAttendanceRuleRequest request) {
        AttendanceRule rule = new AttendanceRule();
        rule.setRuleId(ruleId);
        rule.setName(request.getName());
        rule.setType(request.getType());
        rule.setStartTime(LocalTime.parse(request.getStartTime()));
        rule.setEndTime(LocalTime.parse(request.getEndTime()));
        rule.setRepeatDays(String.join(",", request.getRepeatDays()));
        
        attendanceRuleMapper.updateById(rule);
    }
    
    @Override
    public void deleteAttendanceRule(String ruleId) {
        attendanceRuleMapper.deleteById(ruleId);
    }
}